package com.dlc.yunguang.activitynew;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.commonlibrary.widget.CommonItemView;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.commonlibrary.widget.popup.CommonPopupList;
import com.dlc.yunguang.activity.BuCaoJiaoJieActivity;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.OrderBean;
import com.dlc.yunguang.net.exception.ErrorMsgException;
import com.dlc.yunguang.net.protocol.StockProto;
import com.dlc.yunguang.net.protocol.base.HttpProtocol;
import com.dlc.yunguang.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectOrderActivity extends BaseActivity {
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.drop_order)
    FrameLayout dropOrder;
    @BindView(R.id.item_order)
    CommonItemView itemOrder;
    @BindView(R.id.sure)
    Button sure;

    private CommonPopupList mOrderPopup;//回收单
    List<OrderBean.DataBean.ListBean> OrderList = new ArrayList<>();
    private String OrderId = null;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_select_order;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initRecyclerView();
        titleBar.leftExit(this);
    }

    private void initRecyclerView() {

        mOrderPopup = CommonPopupList.newBuilder(getActivity())
                .setWidthEquals(dropOrder)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        // TODO: 2017/7/20
                        tvOrder.setText(item);
                        if (OrderList != null) {
                            OrderId = OrderList.get(position).id;
                        }
                    }
                })
                .build();

        getOrderData();

    }

    private void getOrderData() {
        showWaitingDialog("请等待",true);
        OrderList.clear();
        StockProto.get().order_deliver("order_deliver", "1", "50", "1", new HttpProtocol.Callback<OrderBean>() {
            @Override
            public void onSuccess(OrderBean hotelBean) {
                dismissWaitingDialog();
                if (hotelBean != null && hotelBean.data != null) {
                    List<String> datas = new ArrayList<>();
                    OrderList = hotelBean.data.list;
                    for (int i = 0; i < hotelBean.data.list.size(); i++) {
                        datas.add(hotelBean.data.list.get(i).orderno);
                    }

                    mOrderPopup.setData(datas);
                }
                if (hotelBean.data.list.size() == 0){
                    Toast.makeText(getActivity(), "没有订单数据", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailed(int code, ErrorMsgException e) {
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                dismissWaitingDialog();
            }
        });


    }

    @OnClick({R.id.drop_order, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_order:
                mOrderPopup.showAsDropDown(view);
                break;
            case R.id.sure:

                Intent intent = new Intent(SelectOrderActivity.this, BuCaoJiaoJieActivity.class);
                intent.putExtra("orderno",tvOrder.getText().toString());
                startActivity(intent);
                break;
        }
    }
}
